# Load these libraries:
#
# library(sf)
# library(mapview)
# library(lubridate)
# library(tidyverse)
# Use classical leaflet/htmlwidgets rendering.
mapviewOptions(fgb = FALSE)
# https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Census-Tracts-2010/5jrd-6zik
#
# Read in the Chicago 2010 Census tracts.
# Rename the ID column.
# Select only the GEOID column.
# Preview a map.
(geo.chi <- st_read('chicago_tracts_2010.geojson') %>%
rename(GEOID = geoid10) %>%
select(GEOID)) %>% mapview(legend = F, col.regions = 'deeppink')
## Reading layer `chicago_tracts_2010' from data source `/Users/erin/Documents/NU GDrive/SICSS/2021/Mapping Lightning Talk/chicago_tracts_2010.geojson' using driver `GeoJSON'
## Simple feature collection with 801 features and 9 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -87.94025 ymin: 41.64429 xmax: -87.52366 ymax: 42.02392
## geographic CRS: WGS 84
# https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-Present/ijzp-q8t2/data
#
# Read in Chicago violent crimes (2020):
# homicide, robbery, criminal sexual assault, battery, & assault.
# Select necessary columns.
# Change Date from mdy_hms string to ymd date.
# Shorten the CSA description to an abbreviation.
# Drop observations with missing coordinates.
# Convert to spatial points geometry.
# Set CRS.
geo.crimes <- read.csv('ViolentCrime2020.csv') %>%
select(ID, Date, Primary.Type, Arrest,
Domestic, Longitude, Latitude) %>%
mutate(Date = mdy_hms(Date) %>% date(),
Primary.Type = ifelse(Primary.Type == 'CRIMINAL SEXUAL ASSAULT',
'CSA',
Primary.Type)) %>%
filter(!is.na(Longitude)) %>%
st_as_sf(coords = c('Longitude', 'Latitude')) %>%
st_set_crs(4326)
# Count by crime type.
geo.crimes %>% as.data.frame() %>% group_by(Primary.Type) %>% tally(n = 'Count')
## # A tibble: 5 x 2
## Primary.Type Count
## <chr> <int>
## 1 ASSAULT 18211
## 2 BATTERY 41422
## 3 CSA 1075
## 4 HOMICIDE 789
## 5 ROBBERY 7849
# Glance at a map for August.
geo.crimes %>% filter(month(Date) == 8) %>%
mapview(cex = .5, lwd = 0, legend = F) +
mapview(geo.chi, alpha.regions = 0, legend = F)
# Read in Cook County population data; take a glimpse.
(df.cook <- read.csv('cook_census.csv')) %>% head()
## GEOID NAME Population
## 1 17031630200 Census Tract 6302, Cook County, Illinois 1825
## 2 17031580700 Census Tract 5807, Cook County, Illinois 5908
## 3 17031590600 Census Tract 5906, Cook County, Illinois 3419
## 4 17031600700 Census Tract 6007, Cook County, Illinois 2835
## 5 17031611900 Census Tract 6119, Cook County, Illinois 1639
## 6 17031804505 Census Tract 8045.05, Cook County, Illinois 3445
# Merge the population data (county) into Chicago tracts. This will drop the
# data for the tracts outside the city.
# Glance at a polygon map (rename the layer; increase the opacity; add heavier borders).
(geo.chi.acs <- geo.chi %>% merge(df.cook)) %>%
mapview(zcol = 'Population', layer.name = 'Population', alpha.regions = .65, lwd = .85)